import request from '/@/utils/request';
import EMsg from '/@/common/eMsg';
import { fa } from 'element-plus/es/locale';
export function Post(route: string, data: any | null = null, $then: Function | undefined = undefined, $catch: Function | undefined = undefined) {
	request
		.request({
			url: route,
			method: 'post',
			data: data!,
		})
		.then((res) => {
			if ($then != undefined) {
				//post全局统一异常提示框输出
				if (res.Code == 1) {
					EMsg.error(res.Message);
				} else {
					$then!(res);
				}
			}
		})
		.catch((error) => {
			if ($catch != undefined) {
				$catch!(error);
			}
		});
}
export function PostAsync(route: string, data: any | null = null) {
	return request.request({
		url: route,
		method: 'post',
		data: data,
	})
}
export function Get(route: string, data: any | null = null) {
	return request.request({
		url: route,
		method: 'Get',
		params: data,
	})
}
export function Put(route: string, data: any | null = null, $then: Function | undefined = undefined, $catch: Function | undefined = undefined) {
	request
		.request({
			url: route,
			method: 'PUT',
			data: data!,
		})
		.then((res) => {
			if ($then != undefined) {
				//PUT全局统一异常提示框输出
				if (res.Code == 1) {
					EMsg.error(res.Message);
				} else {
					$then!(res);
				}
			}
		})
		.catch((error) => {
			if ($catch != undefined) {
				$catch!(error);
			}
		});
}
export function Delete(route: string, data: any | null = null, $then: Function | undefined = undefined, $catch: Function | undefined = undefined) {
	request
		.request({
			url: route,
			method: 'DELETE',
			data: data!,
		})
		.then((res) => {
			if ($then != undefined) {
				//delete全局统一异常提示框输出
				if (res.Code == 1) {
					EMsg.error(res.Message);
				} else {
					$then!(res);
				}
			}
		})
		.catch((error) => {
			if ($catch != undefined) {
				$catch!(error);
			}
		});
}


export function DownLoad(url: string, name: string) {
	return request.request({
		url: url,
		method: 'post',
		data: {},
		responseType: 'arraybuffer',
	})
		.then((res: any) => {
			const url = window.URL.createObjectURL(new Blob([res.data]));
			const link = document.createElement('a');
			link.href = url;
			link.setAttribute('download', name);
			document.body.appendChild(link);
			link.click();
		});
}